package com.peng.leetcode.other;

/**
 * LongestCommonPrefix
 *
 * @author: lupeng6
 * @create: 2020/11/15 15:46
 */
public class LongestCommonPrefix {

    public static void main(String[] args) {
        System.out.println(new Solution().longestCommonPrefix("flower","flow","flight"));
    }

    static class Solution {
        public String longestCommonPrefix(String... strs) {
            String ret = "";
            int index = 0;
            int minLen = strs[0].length();
            for (String str : strs) {
                minLen = Math.min(str.length(), minLen);
            }
            while (index < minLen) {
                boolean flag = true;
                for (int i = 0; i < strs.length - 1; i++) {
                    if (strs[i].charAt(index) != strs[i+1].charAt(index)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    ret += strs[0].charAt(index);
                }

                index++;
            }
            return ret;
        }
    }
}
